/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.procurement.web;

import java.util.List;
import java.util.Set;

import com.javaeye.procurement.dto.MaterialsReceiveOrder;
import com.javaeye.procurement.dto.MaterialsReceiveOrderDetail;
import com.javaeye.procurement.service.IMaterialsReceiveCheckService;
import com.javaeye.common.util.NumberUtils;
import com.javaeye.delivery.dto.OrderCheck;

public class MaterialsReceiveCheckAction extends MaterialsReceiveOrderAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7408989332825417843L;
	
	private OrderCheck checkResult;
	
	private List<Float> acceptNumbers;
	
	private List<Float> otherNumbers;
	
	private List<Integer> detailIds;
	
	private IMaterialsReceiveCheckService receiveCheckService;
	
	public void setReceiveCheckService(IMaterialsReceiveCheckService receiveCheckService) {
		this.receiveCheckService = receiveCheckService;
	}

	/**
	 * 查询需要检验的出货单列表
	 * @return
	 * @throws Exception
	 */
	public String queryMaterialsReceiveCheckList() throws Exception {
		getCondition().setStatus(
				new Integer[]{MaterialsReceiveOrder.ORDER_STATES_RECEIVED});
		return queryMaterialsReceiveOrderList();
	}
	
	/**
	 * 查询用于检验
	 * @return
	 * @throws Exception
	 */
	public String queryMaterialsReceiveForCheck() throws Exception {
		return queryMaterialsReceiveOrderInfo();
	}
	
	/**
	 * 保存检验结果
	 * @return
	 * @throws Exception
	 */
	public String saveOrderCheck() throws Exception {
		MaterialsReceiveOrder order = 
			materialsReceiveOrderService.getMaterialsReceiveOrderInfo(getOrderId());
		Set<MaterialsReceiveOrderDetail> details = order.getOrderDetails();
		double totleAmount = 0;
		for (int i = 0; i < detailIds.size(); i++) {
			int id = detailIds.get(i);
			MaterialsReceiveOrderDetail detail = findDetail(id, details);
			detail.setAcceptNumber(acceptNumbers.get(i));
			detail.setOtherNumber(otherNumbers.get(i));
			//根据实收数量重新计算金额
			double realAmount = (detail.getAcceptNumber() + detail.getOtherNumber())*detail.getPrice();
			totleAmount += realAmount;
			detail.setAmount(NumberUtils.formatDouble(realAmount));
		}
		order.setTotleAmount(NumberUtils.formatDouble(totleAmount));
		
		checkResult.setOrderId(getOrderId());
		receiveCheckService.saveCheckResult(order, checkResult);
		return SUCCESS;
	}
	
	private MaterialsReceiveOrderDetail findDetail(int id, Set<MaterialsReceiveOrderDetail> details) {
		for (MaterialsReceiveOrderDetail detail : details) {
			if (id == detail.getId()) {
				return detail;
			}
		}
		return null;
	}

	public OrderCheck getCheckResult() {
		return checkResult;
	}

	public void setCheckResult(OrderCheck checkResult) {
		this.checkResult = checkResult;
	}

	public List<Float> getAcceptNumbers() {
		return acceptNumbers;
	}

	public void setAcceptNumbers(List<Float> acceptNumbers) {
		this.acceptNumbers = acceptNumbers;
	}

	public List<Float> getOtherNumbers() {
		return otherNumbers;
	}

	public void setOtherNumbers(List<Float> otherNumbers) {
		this.otherNumbers = otherNumbers;
	}

	public void setDetailIds(List<Integer> detailIds) {
		this.detailIds = detailIds;
	}

	public List<Integer> getDetailIds() {
		return detailIds;
	}
}
